﻿var currentPage = 1;
var pageSize = 10;
var status = 'pending';

$(document).ready(function () {

    var today = new Date();

    $(".from.datepicker.search").datepicker({
        //        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 2,
        showOn: "button",
        maxDate: today,
        onClose: function (selectedDate) {
            $(".to.datepicker.search").datepicker("option", "minDate", selectedDate);
        }
    });
    $(".to.datepicker.search").datepicker({
        //        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 2,
        showOn: "button",
        maxDate: today,
        onClose: function (selectedDate) {
            $(".from.datepicker.search").datepicker("option", "maxDate", selectedDate);
        }
    });


    $('#tabs li a').click(function () {
        status = $(this).attr('class');
        loadBookings(1);

        switch (status) {
            case 'deleted':
                $('#btnUndo').show();
                $('#btnDelete').val('Destroy');
                break;
            default:
                $('#btnUndo').hide();
                $('#btnDelete').val('Delete');
                break;
        }

        $('#tabs li.current').removeClass('current');
        $(this).closest('li').addClass('current');
    });

    // add multiple select / deselect functionality
    $('#divBookingList').on('click', '#allChoice', function () {
        $('.choice').prop('checked', this.checked);
    });

    // if all checkbox are selected, check the selectall checkbox
    // and viceversa
    $('#divBookingList').on('click', '.choice', function () {

        if ($(".choice").length == $(".choice:checked").length) {
            $("#allChoice").prop("checked", true);
        } else {
            $("#allChoice").prop("checked", false);
        }
    });

    var openDialog = function () {

        var booking = bookingImplement.getBooking(parseInt($(this).closest('tr').attr('id')));

        loadBookingData(booking);

        $('#approval-dialog').dialog('open');

    };

    $('#divBookingList').on('click', '.button-edit', openDialog);
    $('#divBookingList').on('click', '.button-approve', openDialog);

    $('#btnDelete').click(function () {
        var selectedItems = [];
        $(".choice:checked").each(function () {
            selectedItems.push(parseInt($(this).closest('tr').attr('id')));
        });
        var result = bookingImplement.deleteBookings(selectedItems, status);

        if (result) {
            loadBookings(currentPage);
        }
    });

    $('#btnUndo').click(function () {
        var selectedItems = [];
        $(".choice:checked").each(function () {
            selectedItems.push(parseInt($(this).closest('tr').attr('id')));
        });
        var result = bookingImplement.undoBookings(selectedItems);

        if (result) {
            loadBookings(currentPage);
        }
    });


    $('#btnFind').click(function () { loadBookings(1); });

    loadBookings(1);
});


function loadBookings(pageNum, isPaged) {
    var bookingObjects = bookingImplement.getBookings(status, pageNum,
            $('#keyword').val(),
            $('#fromDate').val(),
            $('#toDate').val());

    if (bookingObjects !== null) {
        $('#divBookingList').setTemplateURL("/Templates/bookingList.htm");
        $('#divBookingList').processTemplate(bookingObjects);

        if (!isPaged) {
            paginator(bookingObjects.RowCount, bookingObjects.PageSize);
        }

        if (bookingObjects.CountPending > 0) {
            $("#countPending").text(' (' + bookingObjects.CountPending + ')');
        }
        else {
            $("#countPending").text('');
        }
        if (bookingObjects.CountApproval > 0) {
            $("#countApproval").text(' (' + bookingObjects.CountApproval + ')');
        }
        else {
            $("#countApproval").text('');
        }
        if (bookingObjects.CountDeleted > 0) {
            $("#countDeleted").text(' (' + bookingObjects.CountDeleted + ')');
        }
        else {
            $("#countDeleted").text('');
        }
    }

    currentPage = pageNum;
}

function paginator(rowCount, pageSize) {
    $('#pager').smartpaginator({
        totalrecords: rowCount,
        recordsperpage: pageSize,
        length: 5,
        next: 'Next',
        prev: 'Prev',
        first: 'First',
        last: 'Last',
        theme: 'blue',
        controlsalways: true,
        onchange: function (newPage) {
            loadBookings(newPage, true);
            currentPage = newPage;
            $('#r').html('Page # ' + newPage);
        }
    });
}